package com.ruoyi.statisticalCenter.controller;

import cn.hutool.core.net.URLEncoder;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.domain.vo.TablePointVo;
import com.ruoyi.common.domain.vo.TableVo;
import com.ruoyi.common.service.TableService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.cache.CustomCacheFilter;
import com.ruoyi.hqfinance.domain.HqNetworkBillDetail;
import com.ruoyi.logistic.domain.PageDate;
import com.ruoyi.logistic.domain.PageInfo;
import com.ruoyi.logistic.utils.TableUtils;
import com.ruoyi.statisticalCenter.domain.ScEnterPaiGrossProfitSheet;
import com.ruoyi.statisticalCenter.domain.ScPaiSet;
import com.ruoyi.statisticalCenter.domain.vo.DateStrVo;
import com.ruoyi.statisticalCenter.domain.vo.ScEnterPaiGrossProfitSheetPointVo;
import com.ruoyi.statisticalCenter.domain.vo.ScEnterPaiGrossProfitSheetVo;
import com.ruoyi.statisticalCenter.service.ScEnterPaiGrossProfitSheetService;
import com.ruoyi.statisticalCenter.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

@RestController
@RequestMapping("/sc/paiGrossProfitSheet")
@Api(tags = "统计中心-进港派件毛利表")
public class ScEnterPaiGrossProfitSheetController {
    @Autowired
    private ScEnterPaiGrossProfitSheetService scEnterPaiGrossProfitSheetService;
    @Autowired
    private HttpServletResponse response;
    @Autowired
    private TableService tableService;
    @Autowired
    private RedisUtil redisUtil;

    //@CustomCacheFilter
    @ApiOperation("汇总")
    @PostMapping("/collect")
    public R collect(@RequestBody ScEnterPaiGrossProfitSheetVo vo) {
        if (vo != null) {
            String startTime = vo.getStartTime().replace("-", "");
            String endTime = vo.getEndTime().replace("-", "");
            vo.setStartTime(startTime);
            vo.setEndTime(endTime);
        }
        String list = scEnterPaiGrossProfitSheetService.selectAll().toString();
        // 构建Redis键
        String redisKey = "sc_enter_pai_gross_profit_sheet:collect:"
                + vo.getStartTime() + ":" + vo.getEndTime()
                + ":" + vo.getNetworkLevel() + list + vo.getPageDate().getPageNum() + vo.getPageDate().getPageSize();
        // 检查Redis中是否存在数据
       /* R redisResult = redisUtil.getObject(redisKey, R.class);
        if (redisResult != null) {
            return redisResult; // 如果Redis中存在数据，则直接返回
        }*/
        // 如果Redis中不存在数据，则从数据库中查询
        R databaseResult = scEnterPaiGrossProfitSheetService.collect(vo);
        
        // 将数据库查询结果存储到Redis，并设置过期时间为一天
        redisUtil.setObject(redisKey, databaseResult, 600);
        return databaseResult;
    }

    @ApiOperation("cache")
    @PostMapping("cache")
    public R cache(@RequestBody DateStrVo dateStrVo) {
        return scEnterPaiGrossProfitSheetService.cache(dateStrVo);
    }

    @ApiOperation("导出")
    @PostMapping("/export")
    public void export(@RequestBody ScEnterPaiGrossProfitSheetVo vo) {
        if (vo != null) {
            String startTime = vo.getStartTime().replace("-", "");
            String endTime = vo.getEndTime().replace("-", "");
            vo.setStartTime(startTime);
            vo.setEndTime(endTime);
            PageDate pageDate = new PageDate();
            pageDate.setPageNum(1);
            pageDate.setPageSize(10000);
            vo.setPageDate(pageDate);
        }
        String fileName = "attachment;filename=" + URLEncoder.createDefault().encode("总部收支汇总报表", Charset.forName("utf-8")) + ".xlsx";
        response.setHeader("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
//        List<ScEnterPaiGrossProfitSheet> list = scEnterPaiGrossProfitSheetService.collectExport(vo);
//        ExcelUtil<ScEnterPaiGrossProfitSheet> util = new ExcelUtil(ScEnterPaiGrossProfitSheet.class);
//        util.exportExcel(response, list, "总部收支汇总报表");
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        scEnterPaiGrossProfitSheetService.collectExport(vo, outputStream);
    }

    @ApiOperation("点击跳转")
    @PostMapping("/point")
    public R point(@RequestBody ScEnterPaiGrossProfitSheetPointVo vo) {
        if (vo != null) {
            String startTime = vo.getStartTime().replace("-", "");
            String endTime = vo.getEndTime().replace("-", "");
            vo.setStartTime(startTime);
            vo.setEndTime(endTime);
        }
        List<HqNetworkBillDetail> list = scEnterPaiGrossProfitSheetService.point(vo);
        List<TableVo> fieldList = tableService.getFieldAnnotation(new HqNetworkBillDetail());
        return TableUtils.handleListResponse(list, fieldList, "暂无数据");
    }

    @ApiOperation("承包区参数配置下拉框")
    @GetMapping("/networkList")
    public R networkList() {
        return R.ok(scEnterPaiGrossProfitSheetService.selectAll());
    }

    @ApiOperation("通过id查询")
    @GetMapping("/findById")
    public R findById(Integer id) {
        return R.ok(scEnterPaiGrossProfitSheetService.findById(id));
    }

    @ApiOperation("修改承包区参数配置")
    @PostMapping("/update")
    public R update(@RequestBody ScPaiSet scPaiSet) {
        Boolean isUpdate = scEnterPaiGrossProfitSheetService.update(scPaiSet);
        if (isUpdate) {
            return R.ok("修改成功");
        } else {
            return R.fail("修改失败");
        }
    }
}
